﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System.Collections.ObjectModel;
using System.Windows.Input;
using HtScrum.Service.Model;
using HtScrum.Service.Data;
using System.Windows.Controls;
using HtScrum.UI.Catalog.Customer;
using System;
using HtScrum.UI.Catalog.Customer.View;

namespace HtScrum.UI.Catalog.Customer.ViewModel
{
    public class CustomerDataCollectionViewModel : ViewModelBase, IServiceProvider
    {
        #region Ctor
        public CustomerDataCollectionViewModel()
        {
            this.ViewModelCollection = new ObservableCollection<CustomerDataItemViewModel>();
            this.DataCollection = new EntityContractCollection<CustomerModel>();
        }
        #endregion

        #region Public Properties
        public ObservableCollection<CustomerDataItemViewModel> ViewModelCollection { get; set; }
        public EntityContractCollection<CustomerModel> DataCollection { get; set; }
        public const string SelectedDataItemPropertyName = "SelectedDataItem";
        private CustomerDataItemViewModel _SelectedDataItem = null;
        public CustomerDataItemViewModel SelectedDataItem
        {
            get
            {
                return _SelectedDataItem;
            }

            set
            {
                if (_SelectedDataItem == value)
                {
                    return;
                }

                var oldValue = _SelectedDataItem;
                _SelectedDataItem = value;

                RaisePropertyChanged(SelectedDataItemPropertyName);
            }
        }
        #endregion

        #region Commands
        public ICommand NewCommand { get { return new RelayCommand(ExecuteNewCommand); } }
        public ICommand EditCommand { get { return new RelayCommand(ExecuteEditCommand); } }
        private void ExecuteNewCommand()
        {
            HtScrum.UI.DialogWindow form = new HtScrum.UI.DialogWindow();
            form.Title = DefaultResources.NewWindowTitle;
            form.SetContent(new CustomerCatalogFormView());
            form.DataContext = new CustomerDataItemViewModel(this)
            {
                EntityModel = new EntityContract<CustomerModel>(new CustomerModel())
            };

            form.Closed += (s, e) =>
            {
                if (form.DialogResult.HasValue && form.DialogResult.Value)
                {
                    var context = (CustomerDataItemViewModel)form.DataContext;
                    this.ViewModelCollection.Add(context);
                    this.DataCollection.Add(context.EntityModel);
                }
            };

            form.Show();
        }
        private void ExecuteEditCommand()
        {
            if (SelectedDataItem != null)
            {
                HtScrum.UI.DialogWindow form = new HtScrum.UI.DialogWindow();
                form.Title = DefaultResources.EditWindowTitle;
                form.SetContent(new CustomerCatalogFormView());
                form.DataContext = this.SelectedDataItem;
                form.Closed += (s, e) =>
                {
                    this.SelectedDataItem.Refresh();
                };
                form.Show();
            }
        }
        #endregion

        #region Internal Methods
        internal void BindingDate()
        {
            foreach (var dataItem in this.DataCollection)
            {
                this.ViewModelCollection.Add(new CustomerDataItemViewModel(this) { EntityModel = dataItem });
            }
        }
        #endregion

        public object GetService(Type serviceType)
        {
            if (this.GetType().IsAssignableFrom(serviceType))
                return this;
            else
                return null;
        }
    }
}